package com.ml.doctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;

import com.google.gson.reflect.TypeToken;
import com.ml.doctor.R;
import com.ml.doctor.bean.LoginBean;
import com.ml.doctor.bean.ZhiChenBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.LocalShared;
import com.ml.doctor.utils.ToastUtil;
import com.ml.doctor.view.BasicItem;
import com.ml.doctor.view.PickSelectThreePop;
import com.ml.doctor.view.PickSelectZCPop;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserInfoSetActivity extends BaseActivity {

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_zhiwu)
    EditText etZhiwu;
    @BindView(R.id.et_keshi)
    EditText etKeshi;
    @BindView(R.id.et_yiyuan)
    EditText etYiyuan;
    @BindView(R.id.bi_zc)
    BasicItem biZc;
    @BindView(R.id.bi_address)
    BasicItem biAddress;
    ZhiChenBean zcBean;
    List<ZhiChenBean> list;
    PickSelectThreePop pickSelectThreePop;
    PickSelectZCPop pickSelectZCPop;
    String tel;
    String pwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info_set);
        ButterKnife.bind(this);
        setTopTitle("身份审核");
        tel = getStringParam("phone");
        pwd = getStringParam("password");
        pickSelectThreePop = new PickSelectThreePop(this);
        pickSelectZCPop = new PickSelectZCPop(this);
        pickSelectThreePop.setConfirmListen(new PickSelectThreePop.ConfirmListen() {
            @Override
            public void onConfirmListen(String str) {
                biAddress.setSubTitle(str);
            }
        });
        pickSelectZCPop.setConfirmListen(new PickSelectZCPop.ConfirmListen() {
            @Override
            public void onConfirmListen(int p) {
                zcBean = list.get(p);
                biZc.setSubTitle(zcBean.rankname);
            }
        });
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        getZCList();
    }

    @OnClick({R.id.bi_zc, R.id.bi_address, R.id.btn_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bi_zc:
                if (list != null && list.size() > 0)
                    pickSelectZCPop.showAtLocation(etName, Gravity.CENTER, 0, 0);
                break;
            case R.id.bi_address:
                pickSelectThreePop.showAtLocation(etName, Gravity.CENTER, 0, 0);
                break;
            case R.id.btn_commit:
                res();
                break;
        }
    }

    private void getZCList() {
        showLoadingDialog("");
        Map<String, String> map = new HashMap<>();
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "docter/selzhicheng", map, new TypeToken<List<ZhiChenBean>>() {
        }.getType(), new NetworkManager.SuccessCallback<List<ZhiChenBean>>() {
            @Override
            public void onSuccess(List<ZhiChenBean> response) {
                list = response;
                pickSelectZCPop.setData(response);
                hideLoadingDialog();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(UserInfoSetActivity.this, message);
                hideLoadingDialog();
            }
        });
    }

    private void res() {
        if (TextUtils.isEmpty(etName.getText().toString())) {
            ToastUtil.showShort(UserInfoSetActivity.this, "姓名不能为空");
            return;
        }
        if (TextUtils.isEmpty(etZhiwu.getText().toString())) {
            ToastUtil.showShort(UserInfoSetActivity.this, "职务不能为空");
            return;
        }
        if (TextUtils.isEmpty(etKeshi.getText().toString())) {
            ToastUtil.showShort(UserInfoSetActivity.this, "科室不能为空");
            return;
        }
        if (TextUtils.isEmpty(etYiyuan.getText().toString())) {
            ToastUtil.showShort(UserInfoSetActivity.this, "医院不能为空");
            return;
        }
        if (TextUtils.isEmpty(biZc.getTitleView().getText().toString())) {
            ToastUtil.showShort(UserInfoSetActivity.this, "请选择职称");
            return;
        }
        if (TextUtils.isEmpty(biAddress.getTitleView().getText().toString())) {
            ToastUtil.showShort(UserInfoSetActivity.this, "请选择地址");
            return;
        }
        showLoadingDialog("验证码获取中");
        Map<String, String> map = new HashMap<>();
        map.put("rankid", zcBean.rankid + "");
        map.put("doctername", etName.getText().toString());
        map.put("tel", tel);
        map.put("adds", biAddress.getTitleView().getText().toString());
        map.put("department", etKeshi.getText().toString());
        map.put("duty", etZhiwu.getText().toString());
        map.put("hosname", etYiyuan.getText().toString());
        map.put("pwd", pwd);
        map.put("online_status", "1");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "login/doadd", map, LoginBean.class, new NetworkManager.SuccessCallback<LoginBean>() {
            @Override
            public void onSuccess(LoginBean response) {
                hideLoadingDialog();
                saveToLocal(response);
                startActivity(new Intent(UserInfoSetActivity.this, MainActivity.class));
                finish();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(UserInfoSetActivity.this, message);
                hideLoadingDialog();
            }
        });
    }

    private void saveToLocal(LoginBean response) {
        LocalShared.getInstance(this).setUserId(response.getDocterid());
        LocalShared.getInstance(this).setUserNick(response.getDoctername());
        LocalShared.getInstance(this).setUserPhone(response.getTel());
    }
}
